package com.winxuan.channel.controller.member;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.winxuan.channel.model.member.Resource;
import com.winxuan.channel.model.object.ChannelResult;
import com.winxuan.channel.service.member.ResourceService;

@Controller
@RequestMapping(value="/resource")
public class ResourceController {

	
	@Autowired
	private ResourceService resourceService;
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public ChannelResult getChannelTreeList(){
		ChannelResult jsonResult = new ChannelResult();
		JSONArray ja = new JSONArray();
		List<Resource> resources = resourceService.loadAllResources();
		for(Resource s : resources){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id",s.getId());
			jsonObject.put("text",s.getName());
			jsonObject.put("url",s.getUrl());
			jsonObject.put("enable",s.isEnable());
			jsonObject.put("sort", s.getSort());
			ja.add(jsonObject);
		}
		jsonResult.setSuccess(true);
		jsonResult.setMsg("。。");
		jsonResult.setResult(ja);
		jsonResult.setTotalCount(100);
		return jsonResult;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ChannelResult addResource(HttpServletRequest request){
		ChannelResult channelResult = new ChannelResult();
		String name = (String) request.getParameter("name");
		String url = (String) request.getParameter("url");
		boolean enable = ((String) request.getParameter("enable")).equals("1")? true : false;
		Integer sort = Integer.valueOf(request.getParameter("sort"));
		
		Resource resource = new Resource();
		resource.setName(name);
		resource.setUrl(url);
		resource.setSort(sort);
		resource.setEnable(enable);
		if(resourceService.addResource(resource)){
			channelResult.setSuccess(true);
			channelResult.setMsg("添加成功!");
		}else{
			channelResult.setSuccess(false);
			channelResult.setMsg("添加失败!");
		}
		return channelResult;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public ChannelResult editResource(@RequestParam("id")Integer id,
			@RequestParam("name")String name,@RequestParam("url")String url,
			@RequestParam("enable")boolean enable,
			@RequestParam("sort")Integer sort){
		ChannelResult channelResult = new ChannelResult();
		Resource resource = resourceService.getResource(id);
		resource.setName(name);
		resource.setUrl(url);
		resource.setSort(sort);
		resource.setEnable(enable);
		resourceService.updateResource(resource);
		channelResult.setSuccess(true);
		return channelResult;
	}
	
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public ChannelResult delResource(@RequestParam("ids")String ids){
		ChannelResult channelResult = new ChannelResult();
		if(ids != null && ids.length() > 0){
			String[] ids2 = ids.split(",");
			for(int i=0;i<ids2.length;i++){
				Resource resource = resourceService.getResource(Integer.valueOf(ids2[i]));
				resourceService.deleteResource(resource);
			}
		}
		channelResult.setSuccess(true);
		return channelResult;
	}

}
